/**
 * Copyright (c) 2012, Hakans Pirates - 
 * Johan Andersson spaiki17@gmail.com, 
 * Mattias Lundberg lundberg.mattias@gmail.com, 
 * Samuel Sjodin sjoodin@gmail.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *  
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package pirates.chat;

import com.jme3.asset.AssetManager;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.network.Client;
import com.jme3.scene.Node;
import java.util.ArrayList;
import java.util.Collections;
import pirates.Settings;
import pirates.network.messages.ChatMessage;
import pirates.notifications.Notify;

/**
 * HUD to display chat messages.
 * 
 * @author Mattias
 */
public class ChatDisplay {

	protected ArrayList<DisplayMessage> messages;
	protected final BitmapFont guiFont;
	protected Node guiNode;
	private final Node chatMessages;
	private final BitmapText messageText;
	private String writing = null;
	private Client client;

	public ChatDisplay(Node guiNode, AssetManager assetManager, Client client) {
		messages = new ArrayList<DisplayMessage>();
		this.guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
		this.guiNode = guiNode;
		this.client = client;
		chatMessages = new Node("chatMessages");

		messageText = new BitmapText(guiFont, false);
		chatMessages.attachChild(messageText);
	}

	/**
	 * User to add chatmessage from network.
	 * 
	 * @param user
	 *            Sending user
	 * @param message
	 *            the chat message
	 */
	public void appendMessage(String user, String message) {
		if (user.equals(Settings.HOST_NAME)) {
			Notify.debug("Chat from myself");
			return;
		}
		appendMessage(user + ": " + message, 0, 20);
	}

	/**
	 * Add local message and send it.
	 */
	public void appendMessage() {
		String message = writing;

		ChatMessage msg = new ChatMessage(Settings.HOST_NAME, message);
		client.send(msg);

		clearWriting();

		appendMessage(Settings.HOST_NAME + ": " + message, 0, 20);
	}

	public void clearWriting() {
		writing = null;
	}

	private void appendMessage(String message, int beginOffset, int endOffset) {
		messages.add(new DisplayMessage(message, beginOffset, endOffset));
		Collections.sort(messages); // Old messages first!
	}

	public void displayMessage(int heightOffset) {
		messageText.setSize(guiFont.getCharSet().getRenderedSize());
		messageText.setLocalTranslation(0, heightOffset, 0);
		messageText.setText(getMessageList());
	}

	private String getMessageList() {
		String messageString = "";

		int numMsgs = 0;

		for (DisplayMessage mess : messages) {
			if (mess.isActive()) {
				messageString += mess.getMessage() + "\n";
				numMsgs++;
			}
			if (numMsgs > 5) {
				break;
			}
		}

		if (writing == null) {
			return messageString;
		}
		return messageString + "\n" + Settings.HOST_NAME + ">" + writing;
	}

	public void attach() {
		guiNode.attachChild(chatMessages);
	}

	public void detach() {
		guiNode.detachChild(chatMessages);
	}

	public void write(char keyChar) {
		write();
		writing = writing + keyChar;
	}

	public void write() {
		if (writing == null) {
			writing = "";
		}
	}

	public void back() {
		writing = writing.substring(0, writing.length() - 2);
	}
}
